package mongong

import (
	"context"
	"gitee.com/baizhige/mongong-go/collections"
	"gitee.com/baizhige/mongong-go/db"
)

func Coll[T collections.CollectionName]() *collections.CollOperations[T] {
	var t T
	res, err := db.Collection(db.DefaultDBKey, t.CollectionName())
	if err != nil {
		panic(err)
	}
	return &collections.CollOperations[T]{
		Collection: res,
	}
}

func InitIndex(entities ...collections.Index) {
	for _, entity := range entities {
		res, err := db.Collection(db.DefaultDBKey, entity.CollectionName())
		if err != nil {
			panic(err)
		}
		_, err = res.Indexes().CreateMany(context.Background(), entity.GetIndex())
		if err != nil {
			panic(err)
		}
	}
}
